﻿


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.Mvc;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Version = Lucene.Net.Util.Version;

namespace SearchDemo.Controllers
{
    public class HomeController : Controller
    {

        /// <summary>
        ///  一元分词
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string txtBody)
        {
            string strResult = AnalyzerResult(txtBody,new StandardAnalyzer(Version.LUCENE_30));
            return Content(strResult);
        }

        public ActionResult TwoAnalyzer()
        {
            return View();
        }

        /// <summary>
        /// 二元分词
        /// </summary>
        /// <returns></returns>
        //[HttpPost]
        //public ActionResult TwoAnalyzer(string txtBody)
        //{
        //    string strResult = AnalyzerResult(txtBody,new CjkAnalyzer());
        //    return Content(strResult);
        //}
        //分词
        private string AnalyzerResult(string txtBody, Analyzer analyzer)
        {

            string ret = "";
            var reader = new System.IO.StringReader(txtBody);
            TokenStream ts = analyzer.TokenStream(txtBody, reader);
            var hasNext = ts.IncrementToken();
            Lucene.Net.Analysis.Tokenattributes.ITermAttribute ita;
            while (hasNext)
            {
                ita = ts.GetAttribute<Lucene.Net.Analysis.Tokenattributes.ITermAttribute>();
                ret += ita.Term + "|";
                hasNext = ts.IncrementToken();
            }
            ts.CloneAttributes();
            reader.Close();
            analyzer.Close();
            return ret;
        }


        #region 盘古分词
        public ActionResult PanGuAnalyzer()
        {
            return View();
        }
        [HttpPost]
        public ActionResult PanGuAnalyzer(string txtBody)
        {
            PanGuLuceneHelper.instance.DeleteAll();//删除全部  

            //PanGuLuceneHelper.instance.Delete("1d");//根据id删除  
            bool exec = true;
            if (exec)
            {
                List<MySearchUnit> list = new List<MySearchUnit>();
                list.Add(new MySearchUnit("1a", "标题小王", "今天是小王的生日，大家都很高兴去他家喝酒，玩了一整天。", "3", "", ""));
                list.Add(new MySearchUnit("1b", "标题小张", "今天是小张的生日，大家都很高兴去他家喝酒，玩了几天。", "3", "", ""));
                list.Add(new MySearchUnit("1c", "标题小王", "今天是小王的生日，大家都很高兴去他家喝酒，玩了一整天。", new Random().Next(1, 10).ToString(), "", ""));
                list.Add(new MySearchUnit("1d", "标题小张", "今天是小张的生日，大家都很高兴去他家喝酒，玩了几天。", new Random().Next(1, 10).ToString(), "", ""));
                PanGuLuceneHelper.instance.CreateIndex(list);//添加索引  
            }
            int count = 0;
            int PageIndex = 1;
            int PageSize = 4;
            string html_content = "";
            List<MySearchUnit> searchlist = PanGuLuceneHelper.instance.Search("3", "小王 生日", PageIndex, PageSize, out count);
            html_content += ("查询结果：" + count + "条数据<br/>");
            if (searchlist == null || searchlist.Count == 0)
            {
                html_content += ("未查询到数据。<br/>");
            }
            else
            {
                foreach (MySearchUnit data in searchlist)
                {
                    html_content += (string.Format("id：{0},title：{1},content：{2},flag：{3},updatetime：{4}<br/>", data.id, data.title, data.content, data.flag, data.updatetime));
                }
            }
            html_content += (PanGuLuceneHelper.instance.version);
            return Content(html_content);
        }
        #endregion

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}